﻿using System;
using System.Collections.ObjectModel;
using System.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Neovolve.Extensibility.VisualStudio.TestProfilerLink.UnitTests
{
    /// <summary>
    ///This is a test class for ConfigurationResolverTests and is intended
    ///to contain all ConfigurationResolverTests Unit Tests
    ///</summary>
    [TestClass]
    public class ConfigurationResolverTests
    {
        /// <summary>
        ///A test for ConfigurationPath
        ///</summary>
        [TestMethod]
        [DeploymentItem("Neovolve.Extensibility.VisualStudio.TestProfilerLink.dll")]
        [DeploymentItem("Test Framework Configuration", "Test Framework Configuration")]
        public void ConfigurationPathTest()
        {
            const string expectedPart = Configuration.FrameworkConfigurationName;
            ConfigurationResolver<TestFramework> target = new ConfigurationResolver<TestFramework>(Configuration.FrameworkConfigurationName);
            String actual = target.ConfigurationPath;

            Assert.IsFalse(String.IsNullOrEmpty(actual), "ConfigurationPath returned an incorrect value.");
            Assert.IsTrue(actual.Contains(expectedPart), "ConfigurationPath did not contain the provided part.");
        }

        /// <summary>
        ///A test for FindByName
        ///</summary>
        [TestMethod]
        public void FindByNameInvalidNameTest()
        {
            ConfigurationResolver<TestFramework> target = new ConfigurationResolver<TestFramework>(Configuration.FrameworkConfigurationName);

            Assert.AreNotEqual(
                0, target.Items.Count, "Invalid number of frameworks available to run test.");

            String name = Guid.NewGuid().ToString();
            TestFramework actual = target.FindByName(name);

            Assert.IsNull(actual, "FindByName returned an invalid value.");
        }

        /// <summary>
        ///A test for FindByName
        ///</summary>
        [TestMethod]
        public void FindByNameTest()
        {
            ConfigurationResolver<TestFramework> target = new ConfigurationResolver<TestFramework>(Configuration.FrameworkConfigurationName);

            Assert.AreNotEqual(
                0, target.Items.Count, "Invalid number of frameworks available to run test.");

            String name = target.Items[0].Name;
            TestFramework expected = target.Items[0];
            TestFramework actual = target.FindByName(name);

            Assert.AreEqual(expected, actual, "FindByName returned an invalid value.");
        }

        /// <summary>
        ///A test for Items
        ///</summary>
        [TestMethod]
        [DeploymentItem("Neovolve.Extensibility.VisualStudio.TestProfilerLink.dll")]
        public void FrameworksTest()
        {
            ConfigurationResolver<TestFramework> target = new ConfigurationResolver<TestFramework>(Configuration.FrameworkConfigurationName);

            Collection<TestFramework> actual = target.Items;
            Int32 expectedCount =
                Directory.GetFiles(target.ConfigurationPath, "*.xml", SearchOption.AllDirectories).Length;

            Assert.AreEqual(expectedCount, actual.Count, "An incorrect number of framework configurations were loaded.");
        }

        /// <summary>
        ///A test for ConfigurationPath
        ///</summary>
        [TestMethod]
        [DeploymentItem("Neovolve.Extensibility.VisualStudio.TestProfilerLink.dll")]
        [DeploymentItem("Test Framework Configuration", "Test Framework Configuration")]
        public void RefreshTest()
        {
            ConfigurationResolver<TestFramework> target = new ConfigurationResolver<TestFramework>(Configuration.FrameworkConfigurationName);

            target.Refresh();

            FrameworksTest();
        }

        /// <summary>
        ///A test for ConfigurationPath
        ///</summary>
        [TestMethod]
        [DeploymentItem("Neovolve.Extensibility.VisualStudio.TestProfilerLink.dll")]
        [DeploymentItem("Test Framework Configuration", "Test Framework Configuration")]
        public void RefreshInternalInvalidPathTest()
        {
            ConfigurationResolver_Accessor<TestFramework> target = new ConfigurationResolver_Accessor<TestFramework>(Configuration.FrameworkConfigurationName);

            String path = @"C:\" + Guid.NewGuid();
            target.RefreshInternal(path);

            // Restore the internal collection
            target.Refresh();
        }

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get;
            set;
        }
    }
}